package com.csii.autocode.utils;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.MultiTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class ConfigurationUtil {
    private static final Logger logger = LoggerFactory.getLogger(ConfigurationUtil.class);

    //传参设置配置信息
    public static Configuration getConfiguration(List<File> paramList, String paramString1, String paramString2) throws IOException {
        Configuration configuration = new Configuration();
        logger.debug(" FileTemplateLoader[] size " + paramList.size());
        logger.debug(" templateRootDirs templateName " + paramString2);
        FileTemplateLoader[] arrayOfFileTemplateLoader = new FileTemplateLoader[paramList.size()];
        for (byte b1 = 0; b1 < paramList.size(); b1++) {
            File file = paramList.get(b1);
            logger.debug(" FileTemplateLoader " + file.getAbsolutePath());
            arrayOfFileTemplateLoader[b1] = new FileTemplateLoader(file);
        }
        MultiTemplateLoader multiTemplateLoader = new MultiTemplateLoader((TemplateLoader[]) arrayOfFileTemplateLoader);
        configuration.setTemplateLoader((TemplateLoader) multiTemplateLoader);
        configuration.setNumberFormat("###############");
        configuration.setBooleanFormat("true,false");
        configuration.setDefaultEncoding(paramString1);
        return configuration;
    }

    //数组变为集合
    public static List<String> getStringList(String paramString1, String paramString2) {
        String[] arrayOfString = getArrayOfString(paramString1, "\\/");
        ArrayList<String> arrayList = new ArrayList();
        arrayList.add(paramString2);
        arrayList.add(File.separator + paramString2);
        String str = "";
        for (byte b1 = 0; b1 < arrayOfString.length; b1++) {
            str = str + File.separator + arrayOfString[b1];
            arrayList.add(str + File.separator + paramString2);
        }
        return arrayList;
    }

    //把容器里面的值转变为String数组
    public static String[] getArrayOfString(String paramString1, String paramString2) {
        if (paramString1 == null)
            return new String[0];
        StringTokenizer stringTokenizer = new StringTokenizer(paramString1, paramString2);
        ArrayList<String> arrayList = new ArrayList();
        while (stringTokenizer.hasMoreElements()) {
            Object object = stringTokenizer.nextElement();
            arrayList.add(object.toString());
        }
        return arrayList.<String>toArray(new String[arrayList.size()]);
    }

    public static String getTemplateString(String paramString, Map<String, Object> paramMap, Configuration paramConfiguration) {
        StringWriter stringWriter = new StringWriter();
        try {
            Template template = new Template("templateString...", new StringReader(paramString), paramConfiguration);
            template.process(paramMap, stringWriter);
            return stringWriter.toString();
        } catch (Exception exception) {
            throw new IllegalStateException("cannot process templateString:" + paramString + " cause:" + exception, exception);
        }
    }

    public static void writeFile(Template paramTemplate, Map<String, Object> paramMap, File paramFile, String paramString) throws IOException, TemplateException {
        FileOutputStream fou = new FileOutputStream(paramFile);
        OutputStreamWriter ouw = new OutputStreamWriter(fou, paramString);
        BufferedWriter bufferedWriter = new BufferedWriter(ouw);
        paramMap.put("Format", new SimpleFormat());
        paramTemplate.process(paramMap, bufferedWriter);
        bufferedWriter.close();
        ouw.close();
        fou.close();
    }
}
